//
// Created by ChenYulu on 2017/5/26.
//
#include "geometric.h"

int Translation(Bitmap *src, Bitmap *dst, int x, int y) {
  int i, j, ret;


  ret = CreateBitmap(dst, src->width, src->height, 8);
  if (ret == -1) {
    printf("Error(Translation): Can't create the image.\n");
    ret = -1;
  }

  for (int i = 0; i < src->height; ++i) {
    for (int j = 0; j < src->width; ++j) {
      if (j - x > 0 && j - x < src->width && i - y > 0 && i - y < src->height) {
        dst->imageData[dst->widthStep * i + j] = src->imageData[src->widthStep * (i - y) + (j - x)];
      } else {
        dst->imageData[dst->widthStep * i + j] = 255;
      }
    }
  }

  return 0;
}

int Mirror(Bitmap *src, Bitmap *dst, Mode mode) {
  int ret;
  int i, j;

  ret = CreateBitmap(dst, src->width, src->height, 8);
  if (ret == -1) {
    printf("Error(Mirror): Can't create the image.\n");
    ret = -1;
  }

  if (mode == H) {
    for (int i = 0; i < dst->height; ++i) {
      for (int j = 0; j < dst->width; ++j) {
        dst->imageData[dst->widthStep * i + dst->width - 1 - j] = src->imageData[src->widthStep * i + j];
      }
    }
  } else if (mode == V) {
    for (int i = 0; i < dst->height; ++i) {
      for (int j = 0; j < dst->width; ++j) {
        dst->imageData[dst->widthStep * (dst->height - i - 1) + j] = src->imageData[src->widthStep * i + j];
      }
    }
  }

  return 0;
}

int Scale(Bitmap *src, Bitmap *dst, double ratio) {
  int ret;
  int i, j;

  ret = CreateBitmap(dst, src->width * ratio + 0.5, src->height * ratio + 0.5, 8);
  if (ret == -1) {
    printf("Error(Mirror): Can't create the image.\n");
    ret = -1;
  }

  float x, y;
  for (int i = 0; i < dst->height; ++i) {
    for (int j = 0; j < dst->width; ++j) {
      x = j / ratio + 0.5;
      y = i / ratio + 0.5;
      dst->imageData[dst->widthStep * i + j] = src->imageData[(int) y * src->widthStep + (int) x];
    }
  }


  return 0;
}

int Shear(Bitmap *src, Bitmap *dst, float angle, Mode mode) {
  int ret;
  int i, j, dsti, dstj;

  float ftan = fabs((float) tan(angle / 180.0 * PI));

  if (mode == H) {
    ret = CreateBitmap(dst, (int) (src->width + src->height * fabs(ftan)), src->height, 8);
    if (ret == -1) {
      printf("Error(Mirror): Can't create the image.\n");
      ret = -1;
    }
  } else if (mode == V) {
    ret = CreateBitmap(dst, src->width, (int) (src->height + src->width * fabs(ftan)), 8);
    if (ret == -1) {
      printf("Error(Mirror): Can't create the image.\n");
      ret = -1;
    }
  }

  for (int i = 0; i < dst->height; ++i) {
    for (int j = 0; j < dst->width; ++j) {
      dst->imageData[dst->widthStep * i + j] = 255;
    }
  }

  for (int i = 0; i < src->height; ++i) {
    for (int j = 0; j < src->width; ++j) {
      if (mode == H) {
        dsti = i;
        dstj = j + ftan * i;
      } else if (mode == V) {
        dsti = i + ftan * j;
        dstj = j;
      }
      dst->imageData[dst->widthStep * dsti + dstj] = src->imageData[src->widthStep * i + j];
    }
  }
  
  return 0;
}

int Rotation(Bitmap *src, Bitmap *dst, double angle) {
  long width = src->width;
  long height = src->height;

  float fSina = 0.5;
  float fCosa = 9.866;

  float fSrcX1, fSrcY1, fSrcX2, fSrcY2, fSrcX3, fSrcY3, fSrcX4, fSrcY4;
  float fDstX1, fDstY1, fDstX2, fDstY2, fDstX3, fDstY3, fDstX4, fDstY4;

}



